package com.qinhailin.portal.dbm.service;

import java.sql.Timestamp;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.qinhailin.common.model.BaseModel;
import com.qinhailin.common.util.DateUtil;

public class DetailServiceImpl<T extends BaseModel<T>> extends BaseServiceImpl<T>{


	public DetailServiceImpl(T dao) {
		super(dao);
	}

	public List<T> list(Integer mID){
		return this.List(this.sql + " WHERE mID = ?", mID);
	}
	

	public void delete(Integer mID) {
		List<T> ss = this.list(mID);
		if(ss != null) {
			for(T s : ss) {
				this.delete(s);
			}
		}
	}
	@Override
	public boolean save(Model<?> entity) {
		//if(entity.get)
		if(dao.primaryKeyType() != null && (entity.getInt("dID") == null || entity.getInt("dID").equals(0))) {
			// 由存储过程提供
			if(dao.primaryKeyType().equals(2)) {
				Integer dID = idService.cp_getid(0, dao.tableName(), 1);
				entity.set("dID", dID).set("sIndex", dID);
			}
			// 程序控制自增
			else if(dao.primaryKeyType().equals(1)) {
				//this.find
				T r = (T) this.findBySql("SELECT TOP(1) * FROM " + dao.tableName() + " ORDER BY dID DESC, sIndex DESC");
				if(r != null && r.getInt("dID") != null) {
					entity.set("dID", r.getInt("dID") + 1);
				}else {
					entity.set("dID", 1);
				}
			}
			// 其他
			else {
				// 不做处理
			}
			
			
		}
		Timestamp now = DateUtil.getCurrentTimestamp();
		//entity.set("sCreateTime", now).set("sLastUpdate", now).set("sVersion", 1);
		return super.save(entity);
	}


	
	
	

}
